<template>
  <a-modal title="更新" :confirm-loading="gitLoading" :visible="true" @cancel="hideUpdate" @ok="updateGit">
  <a-form  class="filer-wrap">
    <a-form-item label="更新选项">
      <a-radio-group v-model:value="updateType" button-style="solid" @change="onChangeRadio">
        <a-tooltip>
          <template #title>将更新Git库，输入commit信息</template>
          <a-radio-button value="git">更新Git</a-radio-button>
        </a-tooltip>
        <a-tooltip>
          <template #title>将更新NPM包和函数库版本，输入版本信息</template>
          <a-radio-button value="npm">更新Npm</a-radio-button>
        </a-tooltip>
      </a-radio-group>
    </a-form-item>
    <a-form-item label="git commit" v-if="updateType === 'git'">
      <a-input v-model:value="commit" placeholder="请输入更新说明"/>
    </a-form-item>
    <a-form-item label="版本说明" v-if="updateType === 'npm'">
      <a-input v-model:value="description" placeholder="请输入更新说明"/>
    </a-form-item>
    <a-form-item label="当前版本" v-if="updateType === 'npm'">
      {{version}}
    </a-form-item>
    <a-form-item label="即将更新为" v-if="updateType === 'npm'">
      {{nextVersion}}
    </a-form-item>
  </a-form>
  </a-modal>

</template>

<script setup>
import {computed, defineProps, onMounted, ref, unref, useContext} from "vue";
import {message} from "ant-design-vue";
import {storage} from "../../../utils/storage";
import {getNextVersion} from "../../../utils/version";
const {emit } = useContext()
let updateType = ref('git')
const commit = ref('')
const description = ref('')
const props = defineProps({
  gitLoading: Boolean,
  latestNpmDescription:String
})
onMounted(()=>{
  description.value = props.latestNpmDescription
})
const onChangeRadio =()=>{
  console.log(updateType.value)
}
const version = computed(()=>{
  return storage.session.get('version') || '0.0.0'
})
const nextVersion = getNextVersion(version.value)
const updateGit=()=>{
  if(!storage.local.get('nigol')){
    message.error('未登录，禁止操作！登录方法翻代码！')
    return
  }
  if(unref(commit) === '' && unref(updateType) === 'git'){
    message.warn('请输入更新说明')
    return
  }
  if(unref(updateType) === 'npm'){
    emit('updateNpm',unref(description))
  }else{
    emit('updateGit',unref(commit))
  }
}
const hideUpdate = ()=>{
  emit('hideUpdate',true)
}
</script>

<style scoped>

</style>
